#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author:liang futing
#@Time :2019/4/4 11:34
import os
import mimetypes

from django.shortcuts import render,HttpResponse,reverse,redirect,get_object_or_404
from django.views.generic import View
from django.http import FileResponse
from django.conf import settings
import xlrd

from web.models import Customer,Payment
from web.forms.customer import CustomerForm

def customer_list(request):
    data_list=Customer.objects.all()
    return render(request,'customer_list.html',{'data_list':data_list})

def customer_add(request):
    if request.method=='GET':
        form=CustomerForm()
        return render(request,'customer_add.html',{'form':form})
    form=CustomerForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect(reverse('web:customer_list'))
    return render(request,'customer_add.html',{'form':form})

def customer_edit(request,pk):
    obj=get_object_or_404(Customer,pk=pk)
    if request.method=='GET':
        form=CustomerForm(instance=obj)
        return render(request,'customer_edit.html',{'form':form})
    form=CustomerForm(data=request.POST,instance=obj)
    if form.is_valid():
        form.save()
        return redirect(reverse('web:customer_list'))
    return render(request,'customer_edit.html',{'form':form})

def customer_del(request,pk):
    Customer.objects.filter(pk=pk).delete()
    return redirect(reverse('web:customer_list'))

def customer_import(request):
    if request.method=="GET":
        return render(request,'customer_import.html')
    context={'status':True,'msg':'导入成功'}
    try:
        customer_excel=request.FILES.get("customer_excel")
        workbook=xlrd.open_workbook(file_contents=customer_excel.file.read())
        sheet=workbook.sheet_by_index(0)
        row_map={
            0:{'text':'客户姓名','name':'name'},
            1:{'text':'年龄','name':'age'},
            2:{'text':'邮箱','name':'email'},
            3:{'text':'公司','name':'company'},
        }
        object_list=[]
        for row_num in range(1,sheet.nrows):
            row=sheet.row(row_num)
            row_dict={}
            for col_num,name_text in row_map.items():
                row_dict[name_text['name']]=row[col_num].value
            object_list.append(Customer(**row_dict))
        Customer.objects.bulk_create(object_list,batch_size=20)
    except Exception as e:
        context['status']=False
        context['msg']='导入失败'
    return render(request,'customer_import.html',context)

def customer_tpl(request):
    tpl_path=os.path.join(settings.BASE_DIR,'web','files','批量导入客户模板.xlsx')
    content_type=mimetypes.guess_type(tpl_path)[0]
    response=FileResponse(open(tpl_path,mode='rb'),content_type=content_type)
    response['Content-Disposition']="attachment;filename=%s"%'customer_excel_tpl.xlsx'
    return response

